package com.gla.service;

import com.gla.dao.OrderMapper;
import com.gla.domain.Order;
import com.gla.domain.OrderDetail;
import com.gla.domain.OrderDetailResponse;
import com.gla.domain.OrderResponse;
import com.gla.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OrderService {
    @Resource
    OrderMapper orderMapper;
    @Resource
    CommodityService commodityService;
    @Autowired
    WalletService walletService;

    public List<Order> select(HashMap<String, Object> map){
        return orderMapper.select(map);
    }

    public Integer insertOrder(Order order) {
        orderMapper.insertOrder(order);
        return order.getId();
    }

    public void insertOrderDetail(OrderDetail orderDetail) {
        orderMapper.insertOrderDetail(orderDetail);
    }

    public List<OrderResponse> getOrderByUserId(Integer user_id,Integer state_id){
        List<OrderResponse> response = new ArrayList<>();
        //获取所有订单
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",user_id);
        if(state_id!=4){
            map.put("state",state_id);
        }

        List<Order> orderList = orderMapper.select(map);
        if(state_id == Order.SHIPPED) {
            map.put("state",Order.CHARGEBACK);
            orderList.addAll(orderMapper.select(map));
        }
        //获取每个订单的详细
        for(int i=0;i<orderList.size();i++){
            //创建订单返回对象
            OrderResponse orderResponse = new OrderResponse();
            orderResponse.setOrder(orderList.get(i));
            //搜索该订单所有详情
            List<OrderDetailResponse> detailList = orderMapper.selectOrderDetailByOrderId(orderList.get(i).getId());
            orderResponse.setList(detailList);
            //加入返回结果列表
            response.add(orderResponse);
        }
        return response;
    }

    public void deleteOrderById(Integer order_id){
        //先删除Detail
        orderMapper.deleteOrderDetailByOrderId(order_id);
        //再删除Order
        orderMapper.deleteOrderById(order_id);
    }

    public Result updateStatus(Integer order_id, Integer state) {
        Map<String, Object> map = new HashMap();
        map.put("order_id",order_id);
        Order order = orderMapper.select(map).get(0);
        //未发货 -> 退单，加库存,退款
        if((order.getState() == Order.PAID) && state == Order.REFUNDED) {
            List<OrderDetailResponse> list = selectOrderDetailById(order_id);
            for(OrderDetailResponse item : list){
                commodityService.updateNumber(item.getCommodity().getId(),
                        item.getCommodity().getNumber() + item.getNumber());
                System.out.println("商品Id:"+item.getCommodity().getId()+"数量变为:"+item.getCommodity().getNumber() + item.getNumber());
            }
            //退还余额
            List<OrderDetailResponse> commodityList = selectOrderDetailById(order_id);
            double totalMoney = 0;
            for(OrderDetailResponse item : commodityList){
                totalMoney += item.getNumber() * item.getCommodity().getPrice();
            }
            walletService.update(order.getUser_id(), totalMoney);
        }
        //未支付->支付,减余额
        else if(order.getState() == Order.PLACED && state == Order.PAID) {
            List<OrderDetailResponse> list = selectOrderDetailById(order_id);
            double totalMoney = 0;
            for(OrderDetailResponse item : list){
                   totalMoney += item.getNumber() * item.getCommodity().getPrice();
            }
            //减钱
            Integer code =walletService.update(order.getUser_id(), totalMoney * -1);
            if(code == -1) {
                return Result.fail("余额不足，请充值!");
            } else if(code == -2){
                return Result.fail("用户不存在,无法查询余额!");
            }
        }

        orderMapper.updateStatus(order_id,state);
        return Result.succeed("修改订单:"+order_id+" 状态为："+state);
    }

    public List<OrderDetailResponse> selectOrderDetailById(Integer order_id){
        List<OrderDetailResponse> list = orderMapper.selectOrderDetailByOrderId(order_id);
        return list;
    }
}
